package main

import (
	"fmt"
	"sync"
	"time"
)

// 全局队列
var queue = make(chan int, 10)
var wg sync.WaitGroup

func producer() {
	for i := 1; i < 11; i++ {
		queue <- i
		fmt.Println("生产：", i)
		time.Sleep(time.Second * 2)
	}
	wg.Done()
}

func consumer() {
	for {
		select {
		case i := <-queue:
			fmt.Println("消费：", i)
			time.Sleep(time.Second)
		default:
			fmt.Println("消息队列为空")
		}
	}
}

func main() {
	for i := 0; i < 5; i++ {
		wg.Add(1)
		go producer()
	}
	for i := 0; i < 3; i++ {
		go consumer()
	}
	wg.Wait()
}
